package com.deer.wms.base.system.service.task.impl;

import com.deer.wms.base.system.dao.bill.BillInDetailMapper;
import com.deer.wms.base.system.dao.task.BillInTaskInfoMapper;
import com.deer.wms.base.system.dao.task.TaskInfoMapper;
import com.deer.wms.base.system.finalUtil.TaskInfoState;
import com.deer.wms.base.system.model.Rocket;
import com.deer.wms.base.system.model.bill.BillInDetail;
import com.deer.wms.base.system.model.box.BoxInfo;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.task.*;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.model.zh.ShelfPlan;
import com.deer.wms.base.system.service.box.BoxInfoService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.task.BillInTaskInfoService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.base.system.util.JedisServiceUtil;
import com.deer.wms.common.core.service.AbstractService;
import com.deer.wms.common.core.text.Convert;
import com.deer.wms.common.utils.StringUtils;
import com.deer.wms.common.utils.TaskNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 任务 服务层实现
 *
 * @author guo
 * @date 2019-06-03
 */
@Service
public class BillInTaskInfoServiceImpl extends AbstractService<BillInTaskInfo, Integer> implements BillInTaskInfoService {

    @Autowired
    private JedisServiceUtil jedis;

    @Autowired
    private TaskNumberUtil taskNumberUtil;

    @Autowired
    private BillInDetailMapper billInDetailMapper;

    @Autowired
    private BillInTaskInfoMapper billInTaskInfoMapper;

    @Autowired
    private ICellInfoService cellInfoService;

    @Autowired
    private IBoxItemService boxItemService;

    @Autowired
    private ITaskInfoService taskInfoService;

    @Autowired
    private BoxInfoService boxInfoService;

    @Autowired
    private PickTaskService pickTaskService;

    @Override
    public List<Ingot> selectIngot(Ingot ingot) {
        return billInTaskInfoMapper.selectIngot(ingot);
    }

    @Override
    public int getCellCountByShelf(Integer shelfId) {
        return billInTaskInfoMapper.getCellCountByShelf(shelfId);
    }

    @Override
    public int getCountByBillIdAndReceipt(TaskInfoCriteria criteria) {
        return billInTaskInfoMapper.getCountByBillIdAndReceipt(criteria);
    }

    @Override
    public List<TaskInfo> selectTaskInfoByBillOutMasterId(Integer billId) {
        return billInTaskInfoMapper.selectTaskInfoByBillOutMasterId(billId);
    }

    @Override
    public TaskInfo getTaskInfoByTaskId(String taskId) {
        return billInTaskInfoMapper.getTaskInfoByTaskId(taskId);
    }

    @Override
    public List<TaskInfoDto> selectTaskInfoByBillInMasterId(Integer billId) {
        return billInTaskInfoMapper.selectTaskInfoByBillInMasterId(billId);
    }

    @Override
    public List<TaskInfoDto> selectTaskInfoByBillOutId(Integer billId) {
        return billInTaskInfoMapper.selectTaskInfoByBillOutId(billId);
    }

    @Override
    public TaskInfoDto selectTaskInfoByid(Integer id) {
        return billInTaskInfoMapper.selectTaskInfoByid(id);
    }

    @Override
    public List<TaskInfoWcs> selectTaskInfoForWcsByState() {
        return billInTaskInfoMapper.selectTaskInfoForWcsByState();
    }

    @Override
    public TaskInfo selectTaskInfoById(Integer id) {
        return billInTaskInfoMapper.selectTaskInfoById(id);
    }

    @Override
    public List<TaskInfo> selectTaskInfoList(TaskInfo taskInfo) {
        return billInTaskInfoMapper.selectTaskInfoList(taskInfo);
    }

    @Override
    public int insertTaskInfo(TaskInfo taskInfo) {
        return billInTaskInfoMapper.insertTaskInfo(taskInfo);
    }

    @Override
    public int insertBillInTaskInfo(TaskInfo taskInfo) {
        return billInTaskInfoMapper.insertBillInTaskInfo(taskInfo);
    }

    @Override
    public int updateTaskInfo(TaskInfo taskInfo) {
        return billInTaskInfoMapper.updateTaskInfo(taskInfo);
    }

    @Override
    public int updateZuPan(TaskInfo taskInfo) {
        return billInTaskInfoMapper.updateZuPan(taskInfo);
    }

    @Override
    public int deleteTaskInfoById(Integer id) {
        return billInTaskInfoMapper.deleteTaskInfoById(id);
    }

    @Override
    public int deleteTaskInfoByIds(String ids) {
        return billInTaskInfoMapper.deleteTaskInfoByIds(Convert.toStrArray(ids));
    }

    @Override
    public List<TaskInfoDto> findList(TaskInfoCriteria criteria) {
        return billInTaskInfoMapper.findList(criteria);
    }

    @Override
    public List<TaskInfoDto> billOutfindList(TaskInfoCriteria criteria) {
        return billInTaskInfoMapper.billOutfindList(criteria);
    }

    @Override
    public List<TaskInfoDto> billInFindList(TaskInfoCriteria criteria) {
        return billInTaskInfoMapper.billInFindList(criteria);
    }

    @Override
    public TaskInfo findByItemCodeAndBatchAndExp(TaskInfoCriteria criteria) {
        return billInTaskInfoMapper.findByItemCodeAndBatchAndExp(criteria);
    }

    @Override
    public List<TaskInfo> selectTaskInfoBybillId(Integer billId) {
        return billInTaskInfoMapper.selectTaskInfoBybillId(billId);
    }

    @Override
    public TaskInfo selectTaskInfoByTaskNumber(long taskNumber) {
        return billInTaskInfoMapper.selectTaskInfoByTaskNumber(taskNumber);
    }

    @Override
    public TaskInfoDto getAllUpdateByTaskNumber(long taskNumber) {
        return billInTaskInfoMapper.getAllUpdateByTaskNumber(taskNumber);
    }

    @Override
    public TaskInfoDto selectTaskInfoByBoxCode(String boxCode) {
        return billInTaskInfoMapper.selectTaskInfoByBoxCode(boxCode);
    }

    @Override
    public TaskInfo selectHaveOldTaskInfoByBoxCode(String boxCode) {
        return billInTaskInfoMapper.selectHaveOldTaskInfoByBoxCode(boxCode);
    }

    @Override
    public Rocket getMessAgeId(Integer id) {
        return billInTaskInfoMapper.getMessAgeId(id);
    }

    @Override
    public boolean reSetCellInfo(TaskInfo taskInfo) {
        return resetPutGoodsAimHaveGoods(taskInfo);
    }

    @Override
    public TaskInfo selectByMaterialId(String materialId) {
        return billInTaskInfoMapper.selectByMaterialId(materialId);
    }

    @Override
    public TaskInfoDto findBillInError(String materialId) {
        return billInTaskInfoMapper.findBillInError(materialId);
    }

    @Override
    public List<TaskInfoDto> findBillInErrorList(TaskInfoCriteria criteria) {
        return billInTaskInfoMapper.findBillInErrorList(criteria);
    }

    @Override
    public List<TaskInfoDto> findTaskInfoList(TaskInfoDto taskInfoDto) {
        return billInTaskInfoMapper.findTaskInfoList(taskInfoDto);
    }

    /**
     * 重新分配货位，在redis中做标记，WCS跟据标记重新分配货位并下发
     * @param taskInfo
     * @return
     */
    public boolean resetPutGoodsAimHaveGoods(TaskInfo taskInfo){
        String taskNumber = String.valueOf(taskInfo.getTaskNumber());
        //String boxCode = taskInfo.getBoxCode();
        boolean result = false;
        boolean handleDBResult = false;
        //String reHandleMoveTask = "reHandleMoveTask:"+ taskInfo.getRoadWay();
        // reHandleInTask = "reHandleInTask:"+ taskInfo.getRoadWay();
        // 修改任务为执行中
        taskInfo.setState(TaskInfoState.STATE_ONT);
        if(taskInfo.getType().equals(TaskInfoState.BILLIN_TYPE)){

           // String readyPutGoodsHaveGoods = "inTaskPutGoodsHaveGoods:".concat(taskNumber).concat(boxCode);
            //String readyPutGoodsHaveGoodsCount = "inTaskPutGoodsHaveGoodsCount:".concat(taskNumber).concat(boxCode);
            // 入库货位重分配
            int i = billInTaskInfoMapper.updateTaskInfo(taskInfo);
            handleDBResult = i > 0;
           // boolean cleanKey = StringUtils.isEmpty(jedis.get(readyPutGoodsHaveGoods)) || jedis.clean(readyPutGoodsHaveGoods);
            //boolean cleanCount = StringUtils.isEmpty(jedis.get(readyPutGoodsHaveGoodsCount)) || jedis.clean(readyPutGoodsHaveGoodsCount);
            //jedis.set(reHandleInTask,String.valueOf(taskInfo.getTaskNumber()));
           //result = cleanKey && cleanCount && handleDBResult;
        }
        return result;
    }
}
